// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
// 将日期转换成数字
function dtConvert(myDate) {
  let year = myDate.getFullYear();
  let month = myDate.getMonth();
  let date = myDate.getDate();
  let hour = myDate.getHours();
  let minute = myDate.getMinutes();
  let DateTime = year * 100000000 + (month + 1) * 1000000 + date * 10000 + hour * 100 + minute;
  return DateTime;
}
// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()
  const _ = db.command
  let pastDate = new Date()
  pastDate.setDate(pastDate.getDate() - 7)// 得到7天前的日期
  let flag = dtConvert(pastDate)// 转换为数字
  try {
      // 查找符合条件的数据
    db.collection("testMain").where({
      state: "solving",
      date: _.lte(flag)
    }).update({
      data: {
        score: 5,
        state: "solved"
      }
    })
  } catch (e) {
    console.log(e)
  }
}